﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FlatRedBall;
using FlatRedBall.Math.Geometry;

using ROMAP.Enums;
using ROMAP.Utilities;

namespace ROMAP.Entities.Tiles
{
    public abstract class Tile : Entity
    {
        #region Constants

        #endregion

        #region Fields

        protected AxisAlignedRectangle _collision;
        protected Sprite _sprite;

        #endregion

        #region Properties

        public override Vector2i TilePosition
        {
            get { return _tilePosition; }
            set
            {
                _tilePosition = value;
                float realX = value.X * TILE_SIZE;
                float realY = -value.Y * TILE_SIZE;

                this.RelativeX = realX;
                this.RelativeY = realY;
            }
        }

        public override int TileX
        {
            get
            {
                _tilePosition.X = (int)Math.Round(this.RelativeX / TILE_SIZE);
                return _tilePosition.X;
            }
            set
            {
                this.RelativeX = value * TILE_SIZE;
                _tilePosition.X = value;
            }
        }

        public override int TileY
        {
            get
            {
                _tilePosition.Y = -(int)Math.Round(this.RelativeY / TILE_SIZE);
                return _tilePosition.Y;
            }
            set
            {
                this.RelativeY = -value * TILE_SIZE;
                _tilePosition.Y = value;
            }
        }

        public AxisAlignedRectangle Collision
        {
            get { return _collision; }
        }

        #endregion

        #region Initialize
        protected override void Initialize()
        {
            base.Initialize();

            _collision = ShapeManager.AddAxisAlignedRectangle();
            _collision.ScaleX = _sprite.ScaleX;
            _collision.ScaleY = _sprite.ScaleY;
            _collision.AttachTo(this, false);
            _collision.Visible = false;
        }

        #endregion

        #region Destroy
        public override void Destroy()
        {
            SpriteManager.RemoveSprite(_sprite);
            if (_collision != null)
            {
                ShapeManager.Remove(_collision);
            }

            base.Destroy();
        }

        #endregion

        public bool Contains(Tile t)
        {
            if (this.GetType().Equals(t.GetType()) && TileX.Equals(t.TileX) && TileY.Equals(t.TileY))
                return true;
            else
                return false;
        }

    }
}
